using System;
using LocalOverride.Fiddler.Store;
using LocalOverride.Fiddler.UI;
using NSubstitute;
using NUnit.Framework;

namespace LocalOverride.Tests.UI
{
    public abstract class MappingEditPresenterFieldTestBase
    {
        private IMappingEditorView view;
        private IMappingStore store;

        [SetUp]
        public void SetUp()
        {
            view = Substitute.For<IMappingEditorView>();
            store = Substitute.For<IMappingStore>();
        }

        private void InitializePresenter(bool onValidate)
        {
            store.Entries.Returns(new MappingEntry[0]);


            if (onValidate)
            {
                store.ValidatorFor(null).ReturnsForAnyArgs(new PassingValidator());
            }
            else
            {
                store.ValidatorFor(null).ReturnsForAnyArgs(new FailingValidator());
            }
            new MappingEditorPresenter(view, store);
        }

        public virtual void EditOverride_SetsEditFieldFromMapping()
        {
            InitializePresenter(true);
            
            MappingEntry mapping = GetValidMapping();
            SetupMappingWithValidValue(mapping);

            view.EditOverride += Raise.Event(view, new EditOverrideEventArgs(mapping));

            AssertViewFieldContainsValidValue(view);
        }

        public virtual void SaveOverrideEdit_AddsEntryToStore_WhenNewOverride()
        {
            InitializePresenter(true);

            view.AddOverride += Raise.Event(view, EventArgs.Empty);
            
            SetupValidView();

            SetupViewWithValidValue(view);

            view.SaveOverrideEdit += Raise.Event(view, EventArgs.Empty);
          
            var mappingEntry = GetValidMapping();

            SetupMappingWithValidValue(mappingEntry);

            store.Received().AddEntry(mappingEntry);
        }

        public virtual void AddOverride_SetsDefaultValues()
        {
            InitializePresenter(true);

            view.AddOverride += Raise.Event(view, EventArgs.Empty);
            
            AssertViewFieldContainsDefaultValue(view);
        }

        

        private void SetupValidView()
        {
            view.EditUrl = @"http://test.com";
            view.EditLocalPath = @"c:\temp\localfile";
            view.IsDirectory = true;
            view.DownloadLocalCopy = true;
            view.EditDefaultFileName = "index.aspx";
        }

        protected abstract void SetupViewWithValidValue(IMappingEditorView view);

        private MappingEntry GetValidMapping()
        {
            const string url = @"http://test.com";
            const string localFile = @"c:\temp\localfile";
            return new MappingEntry()
                       {
                           IsDirectory = true,
                           LocalPath = localFile,
                           Url = url,
                           DownloadLocalCopy = true,
                           DefaultFileName = "index.aspx"
                       };
        }

        protected abstract void AssertViewFieldContainsValidValue(IMappingEditorView mappingEditorView);
        protected abstract void SetupMappingWithValidValue(MappingEntry entry);
        protected abstract void AssertViewFieldContainsDefaultValue(IMappingEditorView mappingEditorView);
    }
}